package com.me.bbs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.*;
import com.me.bbs.dto.AdminDTO;
import com.me.bbs.dto.BanDTO;
import com.me.bbs.entity.Ban;
import com.me.bbs.service.IBanService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-29
 */
@RestController
@RequestMapping("/ban")
public class BanController {
    @Resource
    private IBanService banService;
    @GetMapping("/search")
    public Resp<?> searchUser(@RequestParam("forumId") Long forumId, @RequestParam("q") String q,
                              @RequestParam("current") Long current,
                              @RequestParam("size") Long size) {
        Page<BanDTO> banDTOS = banService.searchUser(forumId, q,current,size);
        return Resp.success(banDTOS);
    }

    @GetMapping("/banDurations")
    public Resp<?> getBanDurations() {
        BanDuration[] values = BanDuration.values();
        List<Map<String ,Object>> maps = new ArrayList<>();
        for (BanDuration value : values) {
            Map<String, Object> m = new HashMap<>();
            m.put("code", value.getCode());
            m.put("detail", value.getDetail());
            maps.add(m);
        }

        return Resp.success(maps);
    }

    @PostMapping("/execute")
    public Resp<?> banUser(@RequestParam("uid") Long uid, @RequestParam("duration") Integer durationCode,
                           @RequestParam("forumId") Long forumId,
                           @RequestParam("reason") String reason) {
        AuthUtils.checkAdmin(AdminType.ADMIN, forumId);
        banService.banUser(uid, durationCode, forumId, reason);
        return Resp.success();
    }
    @DeleteMapping("/execute")
    public Resp<?> unban(@RequestParam("uid") Long uid,
                         @RequestParam("forumId") Long forumId) {
        AuthUtils.checkAdmin(AdminType.ADMIN, forumId);
        boolean remove = banService.lambdaUpdate().eq(Ban::getUser, uid)
                .eq(Ban::getForum, forumId)
                .remove();
        if(remove)
        return Resp.success();
        else
            return Resp.error("记录不存在");
    }
    @GetMapping("/page")
    public Resp<?> page(@RequestParam("forumId") Long forumId,
                        @RequestParam("current") Long current,
                        @RequestParam("size") Long size) {
        Page<BanDTO> banDTOPage = banService.getBanList(forumId, current,size);
        return Resp.success(banDTOPage);
    }

    @GetMapping("/isBanned")
    @RequireLogin
    public Resp<?> isBanned(@RequestParam("forumId") Long forumId, @RequestParam("userId") Long userId ) {
        boolean exists = banService.lambdaQuery().eq(Ban::getUser, userId).eq(Ban::getForum, forumId)
                .gt(Ban::getExpireAt, LocalDateTime.now()).exists();
        return Resp.success(exists);
    }
}
